/** 
 * @Title:  UserUpdatePasswordFragment.java 
 * @author:  Abtswiath丶lxy
 * @data:  2016年6月15日 上午11:08:23 <创建时间>
 * 
 * @history：<以下是历史记录>
 *
 * @modifier: <修改人>
 * @modify date: 2016年6月15日 上午11:08:23 <修改时间>
 * @log: <修改内容>
 *
 * @modifier: <修改人>
 * @modify date: 2016年6月15日 上午11:08:23 <修改时间>
 * @log: <修改内容>
 */
package com.sunmi.usercenter.ui.activity.fragment;

import org.apache.http.Header;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.sunmi.usercenter.Constant;
import com.sunmi.usercenter.R;
import com.sunmi.usercenter.bean.FmInfo;
import com.sunmi.usercenter.bean.Result;
import com.sunmi.usercenter.bean.Shop;
import com.sunmi.usercenter.bean.rpc.ReqUpdatePassword;
import com.sunmi.usercenter.bean.rpc.ReqVerifyPassword;
import com.sunmi.usercenter.ui.component.LoadingDialog;
import com.sunmi.usercenter.utils.MD5Utils;

import android.os.Bundle;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * TODO<请描述这个类是干什么的>
 * 
 * @author Abtswiath丶lxy
 * @versionCode 1 <每次修改提交前+1>
 */
public class UserUpdatePasswordFragment extends SuperFragment implements OnClickListener {

	private View mView;
	private RelativeLayout mRelBack;
	private EditText mEditOldPsd, mEditNewPsd;
	private TextView mTxtOldPsdError, mTxtNewPsdError;
	private LinearLayout mLinShowPsd;
	private Button mBtnOk;
	private ImageView mImgEye;
	private boolean isHidden = true;// 是否隐藏输入框文本
	private Shop mShop;
	private LoadingDialog mLoadingDialog;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		verifyLogin();
		initView();
	}
	
	private void verifyLogin(){
		mShop = mCaCheModel.getShop();
		if(mShop==null){
			openFM(new FmInfo(LoginFragment.class, null));
			onDestroy();
		}
	}
	

	/**
	 * 初始化
	 */
	private void initView() {
		mActivity.SetStatusColorFE542B();
		Constant.CLASS_FRAGMENT = Constant.UserAcountFragment;
		mRelBack = (RelativeLayout) mView.findViewById(R.id.back);
		mEditOldPsd = (EditText) mView.findViewById(R.id.txt_password_old);
		mEditNewPsd = (EditText) mView.findViewById(R.id.txt_password_new);
		mTxtOldPsdError = (TextView) mView.findViewById(R.id.txt_prompt);
		mTxtNewPsdError = (TextView) mView.findViewById(R.id.txt_error);
		mLinShowPsd = (LinearLayout) mView.findViewById(R.id.ln_show_psd);
		mBtnOk = (Button) mView.findViewById(R.id.ok);
		mImgEye = (ImageView) mView.findViewById(R.id.img_show_psd);
		mLoadingDialog = LoadingDialog.createDialog(getActivity(), getString(R.string.waitting));
		mTxtNewPsdError.setVisibility(View.GONE);
		mTxtOldPsdError.setVisibility(View.GONE);
		mLinShowPsd.setOnClickListener(this);
		mBtnOk.setOnClickListener(this);
		mRelBack.setOnClickListener(this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		return mView = inflater.inflate(R.layout.fragment_update_password, container, false);
	}
	
	private void clickOk(){
		String oldPsd = mEditOldPsd.getText().toString();
		String newPsd = mEditNewPsd.getText().toString();
		if(oldPsd.length()<6){
			mTxtOldPsdError.setVisibility(View.VISIBLE);
			return;
		}
		if(newPsd.length()<6){
			mTxtNewPsdError.setVisibility(View.VISIBLE);
			return;
		}
		mLoadingDialog.show();
		ReqUpdatePassword reqUpdatePassword = new ReqUpdatePassword();
		reqUpdatePassword.setNewPassword(MD5Utils.md5Password(mShop.getUserName(), newPsd));
		reqUpdatePassword.setOldPassword(MD5Utils.md5Password(mShop.getUserName(), oldPsd));
		reqUpdatePassword.setuId(mShop.getuId());
		mTxtNewPsdError.setVisibility(View.GONE);
		mTxtOldPsdError.setVisibility(View.GONE);
		mHttpController.updatePassword(reqUpdatePassword, new AsyncHttpResponseHandler() {
			
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				String json = new String(arg2);
//				String json = "{\"code\": 1,\"data\": {},\"msg\": \"\"}";
				Gson gson = new Gson();
				Result result = null;
				try {
					result = gson.fromJson(json, Result.class);
				} catch (Exception e) {
					e.printStackTrace();
					showToast(getString(R.string.req_error));
					return;
				}
				if(result!=null){
					if(result.code==1){
						showToast(getString(R.string.update_success));
						openFM(new FmInfo(UserAcountFragment.class,null));
					}else if(result.code==2){
						mTxtOldPsdError.setVisibility(View.VISIBLE);
					}else{
						showToast(getString(R.string.update_error));
					}
				}else{
					showToast(getString(R.string.update_error));
				}
				mLoadingDialog.dismiss();
			}
			
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
				showToast(getString(R.string.update_error));
				mLoadingDialog.dismiss();
			}
		});
//			ReqVerifyPassword reqVerifyPassword = new ReqVerifyPassword();
//			reqVerifyPassword.setPassword(MD5Utils.md5Password(mShop.getUserName(), oldPsd));
//			reqVerifyPassword.setuId(mShop.getuId());
//			mHttpController.verifyPassword(reqVerifyPassword, new AsyncHttpResponseHandler() {
//				
//				@Override
//				public void onStart() {
//					super.onStart();
//					mLoadingDialog.show();
//				}
//				
//				@Override
//				public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
//					String json = new String(arg2);
//					Gson gson = new Gson();
//					Result result  = null;
//					try {
//						result = gson.fromJson(json, Result.class);
//					} catch (Exception e) {
//						e.printStackTrace();
//						mLoadingDialog.dismiss();
//						showToast(getString(R.string.req_error));
//						return;
//					}
//					if(result!=null&&result.code==1){
//						if(newPsd.length()<6){
//							mTxtNewPsdError.setVisibility(View.VISIBLE);
//							mLoadingDialog.dismiss();
//						}else{
////							updatePsd(newPsd);
//						}
//					}else{
//						mTxtOldPsdError.setVisibility(View.VISIBLE);
//						mLoadingDialog.dismiss();
//					}
//				}
//				@Override
//				public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
//					showToast(getString(R.string.update_error));
//					mLoadingDialog.dismiss();
//				}
//			});
	}
	
//	private void updatePsd(String psd){
//		ReqUpdatePassword reqUpdatePassword = new ReqUpdatePassword();
//		reqUpdatePassword.setPassword(MD5Utils.md5Password(mShop.getUserName(), psd));
//		reqUpdatePassword.setuId(mShop.getuId());
//		mHttpController.updatePassword(reqUpdatePassword, new AsyncHttpResponseHandler() {
//			
//			@Override
//			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
//				String json = new String(arg2);
//				Gson gson = new Gson();
//				Result result  = null;
//				try {
//					result = gson.fromJson(json, Result.class);
//				} catch (Exception e) {
//					e.printStackTrace();
//					mLoadingDialog.dismiss();
//					showToast(getString(R.string.update_error));
//					return;
//				}
//				if(result!=null&&result.code==1){
//					showToast(getString(R.string.update_success));
//					openFM(new FmInfo(UserAcountFragment.class,null));
//				}else{
//					showToast(getString(R.string.update_error));
//				}
//				mTxtNewPsdError.setVisibility(View.GONE);
//				mTxtOldPsdError.setVisibility(View.GONE);
//				mLoadingDialog.dismiss();
//			}
//			
//			@Override
//			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
//				showToast(getString(R.string.update_error));
//				mLoadingDialog.dismiss();
//			}
//		});
//	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ln_show_psd:
			showPsdOrHidePsd();
			break;
		case R.id.ok:
			clickOk();
			break;
		case R.id.back:
			openFM(new FmInfo(UserAcountFragment.class,null));
			break;
		default:
			break;
		}
	}

	/**
	 * 是否显示文本框的内容
	 */
	private void showPsdOrHidePsd() {
		if (isHidden) {
			mEditNewPsd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
			mImgEye.setBackgroundResource(R.drawable.eye_yes);
		} else {
			mEditNewPsd.setTransformationMethod(PasswordTransformationMethod.getInstance());
			mImgEye.setBackgroundResource(R.drawable.eye_no);
		}
		isHidden = !isHidden;
		mEditNewPsd.postInvalidate();
		// 切换后将EditText光标置于末尾
		CharSequence charSequence = mEditNewPsd.getText();
		if (charSequence instanceof Spannable) {
			Spannable spanText = (Spannable) charSequence;
			Selection.setSelection(spanText, charSequence.length());
		}
	}
}
